#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Project : light-edit
@File : subdomain_scan.py
@Author : Yj
@Time : 2025/9/9
@脚本说明 : 子域名扫描模块，扫描指定域名的子域名
"""
import threading
import warnings
import requests

warnings.filterwarnings("ignore")
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
}


def subdomain_scan(domain, sub):
    """扫描指定子域名是否存活"""
    host1 = f"http://{sub}.{domain}"
    host2 = f"https://{sub}.{domain}"
    try:
        response1 = requests.get(host1, headers=headers, timeout=5)
        if response1.status_code in [200, 301, 302, 401, 403, 404]:
            print(f"[+] {sub}.{domain} 存活")
    except:
        try:
            response2 = requests.get(host2, headers=headers, timeout=5)
            if response2.status_code in [200, 301, 302, 401, 403, 404]:
                print(f"[+] {sub}.{domain} 存活")
        except:
            pass


def subdomain_run(domain, subdomain_file):
    """扫描指定域名的子域名"""
    try:
        threads = []
        with open(subdomain_file, mode="r", encoding="utf-8") as f:
            for sub in f:
                sub = sub.strip()
                if sub:
                    t = threading.Thread(target=subdomain_scan, args=(domain, sub))
                    t.start()
                    threads.append(t)
        for t in threads:
            t.join()
    except Exception as e:
        print(f"发生错误：{e}")
